package main

import (
	"github.com/gorilla/websocket"
	"net/http"
	"time"
	"yinlei/go_websocket/impl"
)

var (
	upgrader = websocket.Upgrader{
		//允许跨域
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
)

func wsHandler(w http.ResponseWriter,r *http.Request){
	var (
		wsConn *websocket.Conn
		err error
		data []byte
		conn *impl.Connection
	)
	//Upgrade字段：websocket
	if wsConn,err =upgrader.Upgrade(w,r,nil); err != nil {
		return
	}

	if conn,err = impl.InitConnection(wsConn); err !=nil {
		goto ERR
	}

	/**
	这个是发送心跳
	 */
	go func() {

		var (
			err error
		)
		for{
			if err= conn.WriteMessage([]byte("心跳")); err != nil {
				return
			}
			time.Sleep(1*time.Second)
		}

	}()

	for{
		if data,err = conn.ReadMessage();err!=nil {
			goto ERR
		}
		if err = conn.WriteMessage(data);err!=nil{
			goto ERR
		}
	}


	ERR:
		conn.Close()
}




func main() {
	http.HandleFunc("/ws",wsHandler)
	http.ListenAndServe(":8888",nil)
}
